package edu.mse.camel.client.whiteboard.gef.ui.figures;

import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;

/**
 * Draws a rectangle whose size is determined by the bounds set to it.
 */
public class RectangleFigure extends Shape implements IFigureWithText {

	
private TextFlow textFlow;
/**
 * Creates a RectangleFigure.
 */
public RectangleFigure() { 
	setBorder(new MarginBorder(3));
	FlowPage flowPage = new FlowPage();
	textFlow = new TextFlow();

	textFlow.setLayoutManager(new ParagraphTextLayout(textFlow,
					ParagraphTextLayout.WORD_WRAP_TRUNCATE));

	flowPage.add(textFlow);

	setLayoutManager(new StackLayout());
	add(flowPage);
	
}

/**
 * @see Shape#fillShape(Graphics)
 */
protected void fillShape(Graphics graphics) {
	graphics.fillRectangle(getBounds());
}

/**
 * @see Shape#outlineShape(Graphics)
 */
protected void outlineShape(Graphics graphics) {
	Rectangle r = getBounds();
	int x = r.x + lineWidth / 2;
	int y = r.y + lineWidth / 2;
	int w = r.width - Math.max(1, lineWidth);
	int h = r.height - Math.max(1, lineWidth);
	graphics.drawRectangle(x, y, w, h);
}
public String getText() {
	return textFlow.getText();
}
public void setText(String newText) {
	textFlow.setText(newText);
}

}
